﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraReports.UI;
using RedRiver.Framework.XtraCommon.Extensions;
using RedRiver.Framework.XtraCommon.FormHelper;
using RedRiver.Framework.XtraCommon.Templates;

namespace RedRiver.Framework.XtraCommon.XtraControls{
    /// <summary>
    /// 数据操作工具 工具栏管理器
    /// </summary>
    public class DataToolBarManager : BarManager{
        /// <summary>
        /// 数据编辑类功能
        /// </summary>
        public static readonly Guid DataEditCategory = new Guid("84355296-aed1-466f-b6dc-23d1d80a80a6");

        /// <summary>
        /// 数据导航分类
        /// </summary>
        public static readonly Guid DataNavigatCategory = new Guid("3d68eb83-8739-48a6-8db2-1b5acc9fccff");

        /// <summary>
        /// 数据导航工具栏
        /// </summary>
        private DataNavigatorBar _dataNavigatorBar;

        /// <summary>
        /// 数据工具栏
        /// </summary>
        private DataToolBar _toolBar;

        public DataToolBarManager(){
            InitializeComponent();
        }

        /// <summary>
        /// 数据操作权限规则
        /// </summary>
        public DataManipulateRule ManipulateRule{
            get{
                if (ToolBar != null){
                    return ToolBar.ManipulateRule;
                }
                return DataManipulateRule.None;
            }
            set{
                if (ToolBar != null){
                    ToolBar.ManipulateRule = value;
                }
            }
        }

        #region Bars

        /// <summary>
        /// 数据工具栏
        /// </summary>
        public DataToolBar ToolBar{
            get{
                if (_toolBar == null){
                    _toolBar = new DataToolBar{
                                                  BarName = "ToolBar",
                                                  DockCol = 0,
                                                  DockRow = 0,
                                                  DockStyle = BarDockStyle.Top,
                                                  Text = @"工具栏"
                                              };
                }
                return _toolBar;
            }
        }

        /// <summary>
        /// 数据导航栏
        /// </summary>
        public DataNavigatorBar NavigatorBar{
            get{
                return _dataNavigatorBar ?? (_dataNavigatorBar = new DataNavigatorBar{
                                                                                         BarName = "NavigatorBar",
                                                                                         DockCol = 1,
                                                                                         DockRow = 0,
                                                                                         DockStyle = BarDockStyle.Top,
                                                                                         Offset = 155,
                                                                                         Text = @"导航栏"
                                                                                     });
            }
        }

        #endregion Bars

        protected override void OnLoaded(){
            base.OnLoaded();
            if (!DesignMode){
                this.InitSuperTip();
                if (Categories.IndexOf("数据编辑") < 0){
                    Categories.Add(new BarManagerCategory("数据编辑", DataEditCategory));
                }
                if (Categories.IndexOf("数据导航") < 0){
                    Categories.Add(new BarManagerCategory("数据导航", DataNavigatCategory));
                }
            }
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent(){
            Bars.Clear();
            Bars.AddRange(new Bar[]{
                                       ToolBar,
                                       NavigatorBar
                                   });

            Images = CommonActionImages.ImageList;
            //ToolBar.AddItems();
            //NavigatorBar.AddItems();

            if (StatusBar == null){
                StatusBar = new Bar(this, "StatusBar"){
                                                          BarName = "StatusBar",
                                                          CanDockStyle = BarCanDockStyle.Bottom,
                                                          DockCol = 0,
                                                          DockRow = 0,
                                                          DockStyle = BarDockStyle.Bottom,
                                                          OptionsBar ={
                                                                          AllowQuickCustomization = false,
                                                                          DrawDragBorder = false,
                                                                          UseWholeRow = true
                                                                      },
                                                          Text = @"状态栏",
                                                          Visible = false
                                                      };
            }
        }

        #region Nested type: DataNavigatorBar

        public class DataNavigatorBar : Bar{
            /// <summary>
            /// 移动到第一个
            /// </summary>
            public Action<ItemClickEventArgs> MoveFirst;

            /// <summary>
            /// 移动到末尾
            /// </summary>
            public Action<ItemClickEventArgs> MoveLast;

            /// <summary>
            /// 移动到下一个
            /// </summary>
            public Action<ItemClickEventArgs> MoveNext;

            /// <summary>
            /// 移动到上一个
            /// </summary>
            public Action<ItemClickEventArgs> MovePrevious;

            /// <summary>
            /// 移动到指定位置
            /// </summary>
            public Action<int> MoveTo;

            private int _count;
            private BarButtonItem _moveFirstItem;
            private BarButtonItem _moveLastItem;
            private BarButtonItem _moveNextItem;
            private BarButtonItem _movePreviousItem;

            private int _position;
            private BarEditItem _positionItem;
            private BarStaticItem _countItem;

            public DataNavigatorBar(){
                InitializeComponent();
            }

            /// <summary>
            /// 当前位置 从 0 开始
            /// </summary>
            public int Position{
                get { return _position; }
                set{
                    if ((value >= 0) && (value != _position)){
                        _position = value;
                        RefreshItems();
                    }
                }
            }

            /// <summary>
            /// 总数
            /// </summary>
            public int Count{
                get { return _count; }
                set{
                    if ((value >= 0) && (value != _count)){
                        _count = value;
                        RefreshItems();
                    }
                }
            }

            private bool _itemsAdded;
            /// <summary>
            /// 增加默认部件
            /// </summary>
            public void AddDefaultItems(){
                if (!_itemsAdded){
                    AddItems(new BarItem[]{MoveFirstItem,MovePreviousItem,PositionItem,
                                                       CountItem,
                                                       MoveNextItem,
                                                       MoveLastItem});
                    _itemsAdded = true;
                }
            }

            private void InitializeComponent(){

            }

            /// <summary>
            /// 刷新内部状态
            /// </summary>
            private void RefreshItems(){
                MoveFirstItem.Enabled = Position > 0;
                MovePreviousItem.Enabled = Position > 0;
                MoveNextItem.Enabled = Position < Count - 1;
                MoveLastItem.Enabled = Position < Count - 1;
                PositionItem.Enabled = (Position > -1) && (Count > 0);
                var positionEdit = (RepositoryItemSpinEdit)_positionItem.Edit;
                positionEdit.EditValueChanging -= OnPositionItemOnValueChanged;
                PositionItem.EditValue = Position + 1;
                positionEdit.EditValueChanging += OnPositionItemOnValueChanged;
                CountItem.Caption = string.Format("/{0}", _count);
                ((RepositoryItemSpinEdit)_positionItem.Edit).MaxValue = _count;
            }

            public void OnMoveFirst(object sender, ItemClickEventArgs e){
                Action<ItemClickEventArgs> handler = MoveFirst;
                if (handler != null){
                    handler(e);
                }
            }

            public void OnMoveNext(object sender, ItemClickEventArgs e){
                Action<ItemClickEventArgs> handler = MoveNext;
                if (handler != null){
                    handler(e);
                }
            }

            public void OnPositionCountChanged(int position, int count){
                _position = position;
                _count = count;
                RefreshItems();
            }

            public void OnMoveLast(object sender, ItemClickEventArgs e){
                Action<ItemClickEventArgs> handler = MoveLast;
                if (handler != null){
                    handler(e);
                }
            }

            public void OnMovePrevious(object sender, ItemClickEventArgs e){
                Action<ItemClickEventArgs> handler = MovePrevious;
                if (handler != null){
                    handler(e);
                }
            }

            #region BarItems

            /// <summary>
            /// 导航栏工具项 移动到开头 
            /// </summary>
            public BarButtonItem MoveFirstItem{
                get{
                    if (_moveFirstItem == null){
                        _moveFirstItem = new BarButtonItem{
                                                              Caption = @"到开头",
                                                              Description = @"移动到开头",
                                                              CategoryGuid =
                                                                  DataNavigatCategory,
                                                              Id = 19,
                                                              Name = "MoveFirstItem",
                                                              ImageIndex =
                                                                  CommonActionImages.IndexActionNavigationHistoryBack,
                                                              ItemShortcut = new BarShortcut(Keys.Control | Keys.Home)

                                                          };

                        _moveFirstItem.ItemClick += OnMoveFirst;
                    }
                    return _moveFirstItem;
                }
            }

            /// <summary>
            /// 导航栏工具项 移动到末尾
            /// </summary>
            public BarButtonItem MoveLastItem{
                get{
                    if (_moveLastItem == null){
                        _moveLastItem = new BarButtonItem{
                                                             Caption = @"到末尾",
                                                             Description = @"移动到末尾",
                                                             CategoryGuid =
                                                                 DataNavigatCategory,
                                                             Id = 17,
                                                             Name = "MoveLastItem",
                                                             ImageIndex =
                                                                 CommonActionImages.IndexActionNavigationHistoryForward,
                                                             ItemShortcut = new BarShortcut(Keys.Control | Keys.End)
                                                         };
                        _moveLastItem.ItemClick += OnMoveLast;
                    }
                    return _moveLastItem;
                }
            }

            /// <summary>
            ///  导航栏工具项 移动到下一个
            /// </summary>
            public BarButtonItem MoveNextItem{
                get{
                    if (_moveNextItem == null){
                        _moveNextItem = new BarButtonItem{
                                                             Caption = @"下一个",
                                                             Description = @"移动到下一个",
                                                             CategoryGuid =
                                                                 DataNavigatCategory,
                                                             Id = 18,
                                                             Name = "MoveNextItem",
                                                             ImageIndex =
                                                                 CommonActionImages.IndexActionNavigationNextObject,
                                                             ItemShortcut = new BarShortcut(Keys.Control | Keys.Right)
                                                         };
                        _moveNextItem.ItemClick += OnMoveNext;
                    }
                    return _moveNextItem;
                }
            }

            /// <summary>
            /// 导航栏工具项 移动到上一个
            /// </summary>
            public BarButtonItem MovePreviousItem{
                get{
                    if (_movePreviousItem == null){
                        _movePreviousItem = new BarButtonItem{
                                                                 Caption = @"上一个",
                                                                 CategoryGuid =
                                                                     DataNavigatCategory,
                                                                 Id = 16,
                                                                 Name = "MovePreviousItem",
                                                                 Description = @"移动到上一个",
                                                                 ImageIndex =
                                                                     CommonActionImages.
                                                                     IndexActionNavigationPreviousObject,
                                                                 ItemShortcut = new BarShortcut(Keys.Control | Keys.Left)
                                                             };
                        _movePreviousItem.ItemClick += OnMovePrevious;
                    }
                    return _movePreviousItem;
                }
            }

            /// <summary>
            /// 导航栏文本项，显示当前位置/总数
            /// </summary>
            public BarEditItem PositionItem {
                get{
                    if (_positionItem == null){
                        _positionItem = new BarEditItem{
                                                             Caption = @"位置",
                                                             CategoryGuid =
                                                                 DataNavigatCategory,
                                                             Id = 22,
                                                             Description = @"显示当前位置",
                                                             Name = "PositionItem",
                                                             Edit = new RepositoryItemSpinEdit{MaxValue = Count,MinValue = 0,NullText = string.Empty,IsFloatValue = false}
                                                         };
                        var positionEdit = ((RepositoryItemSpinEdit)_positionItem.Edit);
                        positionEdit.Buttons.Clear();
                        positionEdit.ValueChanged += OnPositionItemOnValueChanged;
                    }
                    return _positionItem;
                }
            }

            private void OnPositionItemOnValueChanged(object s, EventArgs e){
                if (MoveTo != null){
                    MoveTo(Convert.ToInt32(Math.Round(((SpinEdit)s).Value, 0)));
                }
            }

            /// <summary>
            /// 导航栏文本项，显示当前总数
            /// </summary>
            public BarStaticItem CountItem {
                get {
                    if (_countItem == null) {
                        _countItem = new BarStaticItem {
                            Caption = @"总数",
                            CategoryGuid = DataNavigatCategory,
                            Id = 23,
                            Description = @"显示当前集合总数",
                            Name = "CountItem",
                            TextAlignment = StringAlignment.Near
                        };
                    }
                    return _countItem;
                }
            }

            #endregion BarItems
        }

        #endregion

        #region Nested type: DataToolBar

        public class DataToolBar : Bar{
            private BarButtonItem _barItemClose;
            private BarButtonItem _barItemDelete;
            private BarButtonItem _barItemEdit;
            private BarButtonItem _barItemNew;
            private BarButtonItem _barItemRefresh;
            private BarButtonItem _barItemSave;
            private BarButtonItem _barItemSaveAndClose;
            private BarButtonItem _barButtonItemValidate;
            private BarButtonItem _barButtonItemView;
            private BarButtonItem _barItemPrintCurrent;
            private BarButtonItem _barItemPrintPreview;
            private BarButtonItem _barItemShowReportDesigner;
            private BarButtonItem _barItemAudit;
            private DataManipulateRule _manipulateRule;

            public DataToolBar(){
                InitializeComponent();
            }

            /// <summary>
            /// 数据操作权限
            /// </summary>
            public DataManipulateRule ManipulateRule{
                get { return _manipulateRule; }
                set{
                    if (_manipulateRule != value){
                        _manipulateRule = value;
                        OnManipulateRuleChanged();
                    }
                }
            }

            #region BarButtonItem

            /// <summary>
            /// 关闭窗口 按钮工具按钮
            /// </summary>
            public BarButtonItem BarItemClose{
                get{
                    if (_barItemClose == null){
                        _barItemClose = new BarButtonItem{
                                                                   Caption = @"关闭(&X)",
                                                                   Description = @"关闭窗口",
                                                                   Id = 15,
                                                                   Name = "BarItemClose",
                                                                   ImageIndex =
                                                                       CommonActionImages.IndexActionClose
                                                               };
                        _barItemClose.ItemClick += (s, e) =>{
                                                             if (FormClose != null){
                                                                 FormClose();
                                                             }
                                                         };
                    }
                    return _barItemClose;
                }
            }

            /// <summary>
            /// 删除当前数据工具按钮
            /// </summary>
            public BarButtonItem BarItemDelete{
                get{
                    if (_barItemDelete == null){
                        _barItemDelete = new BarButtonItem{
                                                                    Caption = @"删除(&D)",
                                                                    CategoryGuid =
                                                                        DataEditCategory,
                                                                    Id = 16,
                                                                    ItemShortcut =
                                                                        new BarShortcut((Keys.Control | Keys.D)),
                                                                    Name = "BarItemDelete",
                                                                    Description = @"删除当前数据",
                                                                    ImageIndex =
                                                                        CommonActionImages.IndexActionDelete
                                                                };
                        _barItemDelete.ItemClick += (s, e) =>{
                                                              if (DeleteData != null){
                                                                  DeleteData();
                                                              }
                                                          };
                    }
                    return _barItemDelete;
                }
            }

            /// <summary>
            /// 编辑当前数据工具按钮
            /// </summary>
            public BarButtonItem BarItemEdit{
                get{
                    if (_barItemEdit == null){
                        _barItemEdit = new BarButtonItem{
                                                                  Caption = @"编辑(&E)",
                                                                  CategoryGuid =
                                                                      DataEditCategory,
                                                                  Description = @"编辑当前数据",
                                                                  Id = 4,
                                                                  ItemShortcut =
                                                                      new BarShortcut((Keys.Control | Keys.E)),
                                                                  Name = "BarItemEdit",
                                                                  ImageIndex =
                                                                      CommonActionImages.IndexActionEdit,
                                                              };
                        _barItemEdit.ItemClick += (s, e) =>{
                                                            if (DataEdit != null){
                                                                DataEdit();
                                                            }
                                                        };
                    }
                    return _barItemEdit;
                }
            }

            /// <summary>
            /// 新建条目工具按钮
            /// </summary>
            public BarButtonItem BarItemNew{
                get{
                    if (_barItemNew == null){
                        _barItemNew = new BarButtonItem{
                                                                 Caption = @"新建(&N)",
                                                                 CategoryGuid = DataEditCategory,
                                                                 Description = @"新建条目",
                                                                 Id = 1,
                                                                 ItemShortcut = new BarShortcut((Keys.Control | Keys.N)),
                                                                 Name = "BarItemNew",
                                                                 ImageIndex = CommonActionImages.IndexActionNew
                                                             };
                        _barItemNew.ItemClick += (s, e) =>{
                                                           if (AddNewData != null){
                                                               AddNewData();
                                                           }
                                                       };
                    }

                    return _barItemNew;
                }
            }

            /// <summary>
            /// 刷新数据工具按钮
            /// </summary>
            public BarButtonItem BarItemRefresh{
                get{
                    if (_barItemRefresh == null){
                        _barItemRefresh = new BarButtonItem{
                                                                     Caption = @"刷新(&R)",
                                                                     CategoryGuid =
                                                                         DataEditCategory,
                                                                     Description = @"刷新数据",
                                                                     Id = 13,
                                                                     ItemShortcut =
                                                                         new BarShortcut((Keys.Control | Keys.R)),
                                                                     Name = "BarItemRefresh",
                                                                     ImageIndex = CommonActionImages.IndexActionRefresh
                                                                 };
                        _barItemRefresh.ItemClick += (s, e) =>{
                                                               if (RefreshData != null){
                                                                   RefreshData();
                                                               }
                                                           };
                    }

                    return _barItemRefresh;
                }
            }

            /// <summary>
            /// 保存数据工具按钮
            /// </summary>
            public BarButtonItem BarItemSave{
                get{
                    if (_barItemSave == null){
                        _barItemSave = new BarButtonItem{
                                                                  Caption = @"保存(&S)",
                                                                  Description = @"保存数据",
                                                                  Id = 14,
                                                                  ItemShortcut =
                                                                      new BarShortcut((Keys.Control | Keys.S)),
                                                                  Name = "BarItemSave",
                                                                  ImageIndex =
                                                                      CommonActionImages.IndexActionSave,
                                                                  
                                                              };
                        _barItemSave.ItemClick += (s, e) =>{
                                                            if (SaveData != null){
                                                                SaveData();
                                                            }
                                                        };
                    }
                    return _barItemSave;
                }
            }

            /// <summary>
            /// 浏览表格明细
            /// </summary>
            public BarButtonItem BarItemView{
                get{
                    if (_barButtonItemView == null){
                        _barButtonItemView = new BarButtonItem{
                                                                  Caption = @"查看(&V)",
                                                                  CategoryGuid =
                                                                      DataEditCategory,
                                                                  Description = @"浏览表格明细",
                                                                  Id = 5,
                                                                  ItemShortcut =
                                                                      new BarShortcut((Keys.Control | Keys.V)),
                                                                  Name = "BarItemView",
                                                                  ImageIndex =
                                                                      CommonActionImages.IndexActionOpenObject
                                                              };
                        _barButtonItemView.ItemClick += (s, e) =>{
                                                            if (ViewData != null){
                                                                ViewData();
                                                            }
                                                        };
                    }
                    return _barButtonItemView;
                }
            }

            /// <summary>
            /// 验证数据 确认当前编辑工具按钮
            /// </summary>
            public BarButtonItem BarItemValidate{
                get{
                    if (_barButtonItemValidate == null){
                        _barButtonItemValidate = new BarButtonItem{
                                                                      Caption = @"确认编辑",
                                                                      CategoryGuid =
                                                                          DataEditCategory,
                                                                      Description = @"确认当前编辑",
                                                                      Id = 26,
                                                                      Name = "BarButtonItemValidate",
                                                                      ImageIndex =
                                                                          CommonActionImages.IndexStateValidationValid
                                                                  };
                        _barButtonItemValidate.ItemClick += (s, e) =>{
                                                                if (DataValidate != null){
                                                                    DataValidate();
                                                                }
                                                            };
                    }
                    return _barButtonItemValidate;
                }
            }

            /// <summary>
            /// 保存数据并关闭窗口工具按钮
            /// </summary>
            public BarButtonItem BarItemSaveAndClose{
                get{
                    if (_barItemSaveAndClose == null){
                        _barItemSaveAndClose = new BarButtonItem{
                                                                          Caption = @"保存并关闭",
                                                                          CategoryGuid = DataEditCategory,
                                                                          Description = @"保存数据并关闭窗口",
                                                                          Id = 24,
                                                                          Name = "BarItemSaveAndClose",
                                                                          ImageIndex =
                                                                              CommonActionImages.IndexActionSaveClose
                                                                      };

                        _barItemSaveAndClose.ItemClick += (s, e) =>{
                                                                    if (SaveAndClose != null){
                                                                        SaveAndClose();
                                                                    }
                                                                };
                    }

                    return _barItemSaveAndClose;
                }
            }

            /// <summary>
            /// 打印当前记录工具按钮
            /// </summary>
            public BarButtonItem BarItemPrintCurrent{
                get{
                    if (_barItemPrintCurrent == null){
                        _barItemPrintCurrent = new BarButtonItem{
                                                                          Caption = @"打印",
                                                                          Description = @"打印当前记录",
                                                                          Id = 27,
                                                                          Name = "BarItemPrintCurrent",
                                                                          ImageIndex =
                                                                              CommonActionImages.
                                                                              IndexActionPrintingPrint,
                                                                          ItemShortcut = new BarShortcut(Keys.Control|Keys.P)
                                                                      };
                        _barItemPrintCurrent.ItemClick += (s, e) =>{
                                                                    if (PrintCurrent != null){
                                                                        Manager.Form.BeginInvoke(PrintCurrent);
                                                                    }};
                    }
                    return _barItemPrintCurrent;
                }
            }

            /// <summary>
            /// 打印预览工具按钮
            /// </summary>
            public BarButtonItem BarItemPrintPreview{
                get{
                    if(_barItemPrintPreview == null){
                        _barItemPrintPreview = new BarButtonItem{Caption = @"打印预览",
                                                                         Description = @"打印预览",
                                                                         Id = 28,
                                                                         Name = "BarItemPrintPreview",
                                                                         ImageIndex =
                                                                             CommonActionImages.IndexActionPrintingPreview,
                                                                             ItemShortcut = new BarShortcut(Keys.Control|Keys.Alt|Keys.P)
                        };
                        _barItemPrintPreview.ItemClick += (s, e) => {
                            if (PrintPreview != null) {
                                Manager.Form.BeginInvoke(PrintPreview);
                            }
                        };
                    }
                    return _barItemPrintPreview;
                }
            }

            /// <summary>
            /// 打开报表设计器 工具按钮
            /// </summary>
            public BarButtonItem BarItemShowShowReportDesigner{
                get { if (_barItemShowReportDesigner == null){
                    _barItemShowReportDesigner = new BarButtonItem{
                        Caption = @"设计报表",
                        Description = @"设计当前窗体默认报表",
                        Id = 28,
                        Name = "BarItemShowReportDesigner",
                        ImageIndex =
                            CommonActionImages.IndexActionReportShowDesigner
                    };
                    _barItemShowReportDesigner.ItemClick += (s,e) =>{
                        if (ShowReportDesigner!=null) {
                            ShowReportDesigner();}};
                }
                    return _barItemShowReportDesigner;
                }
            }

            /// <summary>
            /// 审核 工具按钮
            /// </summary>
            public BarButtonItem BarItemAudit{get{
                if(_barItemAudit == null){
                    _barItemAudit = new BarButtonItem{
                                                                 Caption = @"审核",
                                                                 Description = @"审核当前单据",
                                                                 Id = 29,
                                                                 Name = "BarItemAudit",
                                                                 ImageIndex = CommonActionImages.IndexActionAudit,
                                                                 ItemShortcut = new BarShortcut(Keys.Control|Keys.Alt|Keys.A)
                                                             };
                    _barItemAudit.ItemClick += (s, e) =>{
                        if (AuditVouch != null) {
                            AuditVouch();}};
                }
                return _barItemAudit;
            }}

            #endregion BarButtonItem

            #region Internal Function

            /// <summary>
            /// 向 <see cref="BarManager"/>  增加 <see cref="BarItem"/>
            /// </summary>
            internal void AddItems(){
                Manager.Items.AddRange(new BarItem[]{
                                                        BarItemNew,
                                                        BarItemEdit,
                                                        BarItemView,
                                                        BarItemRefresh,
                                                        BarItemSave,
                                                        BarItemClose,
                                                        BarItemDelete,
                                                        BarItemSaveAndClose,
                                                        BarItemValidate,
                                                        BarItemPrintCurrent,
                                                        BarItemPrintPreview
                                                    });
            }

            private void InitializeComponent(){
                /* LinksPersistInfo.AddRange(new[]{
                                                   new LinkPersistInfo(BarItemNew, true),
                                                   new LinkPersistInfo(BarItemEdit),
                                                   new LinkPersistInfo(BarItemView),
                                                   new LinkPersistInfo(BarItemDelete),
                                                   new LinkPersistInfo(BarItemRefresh),
                                                   new LinkPersistInfo(BarItemSave),
                                                   new LinkPersistInfo(BarItemClose)
                                               });
                */
            }

            #endregion Internal Function

            public void OnManipulateRuleChanged(){
                BarItemValidate.Enabled =
                    BarItemEdit.Enabled =
                    BarItemSaveAndClose.Enabled = BarItemSave.Enabled = Assert(DataManipulateRule.AllowEdit);
                BarItemNew.Enabled = Assert(DataManipulateRule.AllowNew);
                BarItemDelete.Enabled = Assert(DataManipulateRule.AllowRemove);
                BarItemRefresh.Enabled =
                    BarItemView.Enabled = Assert(DataManipulateRule.AllowView | DataManipulateRule.AllowEdit);
                BarItemView.Enabled = Assert(DataManipulateRule.AllowView);
            }

            private bool Assert(DataManipulateRule rule){
                return (ManipulateRule & rule) != 0;
            }

            #region Event Process

            /// <summary>
            /// 新建记录事件
            /// </summary>
            public Action AddNewData;

            /// <summary>
            /// 编辑当前记录事件
            /// </summary>
            public Action DataEdit;

            /// <summary>
            /// 验证数据
            /// </summary>
            public Action DataValidate;

            /// <summary>
            /// 删除当前项
            /// </summary>
            public Action DeleteData;

            /// <summary>
            /// 关闭窗口事件
            /// </summary>
            public Action FormClose;

            /// <summary>
            /// 刷新数据
            /// </summary>
            public Action RefreshData;

            /// <summary>
            /// 保存数据并关闭窗口
            /// </summary>
            public Action SaveAndClose;

            /// <summary>
            /// 保存数据
            /// </summary>
            public Action SaveData;

            /// <summary>
            /// 浏览数据事件
            /// </summary>
            public Action ViewData;

            /// <summary>
            /// 打印当前数据
            /// </summary>
            public Action PrintCurrent;

            /// <summary>
            /// 打印预览
            /// </summary>
            public Action PrintPreview;

            /// <summary>
            /// 显示报表设计器
            /// </summary>
            public Action ShowReportDesigner;

            /// <summary>
            /// 获取当前窗体默认报表
            /// <remarks>默认报表需要自行加载数据源</remarks>
            /// 默认报表操作中通过该方法获取当前窗体的默认报表并进行操作
            /// 相关方法为
            /// <see cref="PrintPreview"/>
            /// <see cref="PrintCurrent"/>
            /// </summary>
            public Func<XtraReport> GetDefaultReport; 

            /// <summary>
            /// 审核单据
            /// </summary>
            public Action AuditVouch;

            #endregion Event Process
        }

        #endregion
    }
}